package com.sunday.pinxinghui.model.mine;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.sunday.common.base.BaseActivity;
import com.sunday.pinxinghui.R;


import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/27.
 */

public class CouponListActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private List<String> titles = new ArrayList<>();
    private int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);
        ButterKnife.bind(this);
        titleView.setText("我的卡券");
        page=getIntent().getIntExtra("page",0);
        initView();
    }

    private void initView(){
        titles.add("优惠券");
        titles.add("电子券");
        initFragment();
    }
    private ArrayList<Fragment> fragments=new ArrayList<>();
    private void initFragment(){
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(page,false);

    }

    public class PagerAdapter extends FragmentPagerAdapter {

        private Fragment[] fragments;

        public PagerAdapter(FragmentManager fm) {
            super(fm);
            fragments=new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position]==null){
                if (position==0){
                    fragments[position]= CouponListFragment.newInstance(1);
                }else {
                    fragments[position]= CouponListFragment.newInstance(2);
                }
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

}
